package labox.innovation.gameserver.skills.conditions;

import labox.innovation.gameserver.skills.Env;

/**
 * @author mkizub
 * 
 */
public class ConditionLogicOr extends Condition {

	private static Condition[] _emptyConditions = new Condition[0];
	public Condition[] conditions = _emptyConditions;

	public void add(Condition condition) {
		if (condition == null)
			return;
		if (getListener() != null)
			condition.setListener(this);
		final int len = conditions.length;
		final Condition[] tmp = new Condition[len + 1];
		System.arraycopy(conditions, 0, tmp, 0, len);
		tmp[len] = condition;
		conditions = tmp;
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.skills.conditions.Condition#setListener(labox.innovation.gameserver.skills.conditions.ConditionListener)
	 */
	@Override
	void setListener(ConditionListener listener) {
		if (listener != null) {
			for (Condition c : conditions)
				c.setListener(this);
		} else {
			for (Condition c : conditions)
				c.setListener(null);
		}
		super.setListener(listener);
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.skills.conditions.Condition#testImpl(labox.innovation.gameserver.skills.Env)
	 */
	@Override
	public boolean testImpl(Env env) {
		for (Condition c : conditions) {
			if (c.test(env))
				return true;
		}
		return false;
	}

}
